﻿using Microsoft.AspNetCore.Mvc.Rendering;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace CarparkIMS_CMS.Models
{
    public static class EnumHelper
    {
        public static List<SelectListItem> GetEnumList<TEnum>(string firstItemDescription)
        {
            List<SelectListItem> ListBag = new List<SelectListItem>();
            if (firstItemDescription != "")
            {
                ListBag.Add(new SelectListItem { Text = "----- " + firstItemDescription + " -----", Value = "" });
            }
            Array sourceArray = Enum.GetValues(typeof(TEnum));
            foreach (var item in sourceArray)
            {
                ListBag.Add(new SelectListItem
                {
                    Text = GetEnumDescription(item),
                    Value = GetEnumDescription(item)
                });
            }
            return ListBag;
        }

        public static List<SelectListItem> GetEnumList_item<TEnum>(string firstItemDescription)
        {
            List<SelectListItem> ListBag = new List<SelectListItem>();
            if (firstItemDescription != "")
            {
                ListBag.Add(new SelectListItem { Text = "----- " + firstItemDescription + " -----", Value = "" });
            }
            Array sourceArray = Enum.GetValues(typeof(TEnum));
            foreach (var item in sourceArray)
            {
                ListBag.Add(new SelectListItem
                {
                    Text = GetEnumDescription(item),
                    Value = item.ToString()
                });
            }
            return ListBag;
        }

        public static List<SelectListItem> GetEnumList_int<TEnum>(string firstItemDescription)
        {
            List<SelectListItem> ListBag = new List<SelectListItem>();
            if (firstItemDescription != "")
            {
                ListBag.Add(new SelectListItem { Text = "----- " + firstItemDescription + " -----", Value = "" });
            }
            Array sourceArray = Enum.GetValues(typeof(TEnum));
            foreach (var item in sourceArray)
            {
                ListBag.Add(new SelectListItem
                {
                    Text = GetEnumDescription(item),
                    Value = ((int)item).ToString()
                });
            }
            return ListBag;
        }

        public static List<SelectListItem> GetEmptyList(string firstItemDescription)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            if (!String.IsNullOrEmpty(firstItemDescription))
                list.Add(new SelectListItem { Text = "----- " + firstItemDescription + " -----", Value = "" });

            return list;
        }

        public static string GetEnumDescription<TEnum>(TEnum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            if (fi != null)
            {
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if ((attributes != null) && (attributes.Length > 0))
                    return attributes[0].Description;
                else
                    return value.ToString();
            }
            else
            {
                return "";
            }
        }

        public static string GetDescriptionFromEnumValue(Enum value)
        {
            DescriptionAttribute attribute = value.GetType()
                .GetField(value.ToString())
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .SingleOrDefault() as DescriptionAttribute;
            return attribute == null ? value.ToString() : attribute.Description;
        }

        public static T GetEnumValueFromDescription<T>(string description)
        {
            var type = typeof(T);
            if (!type.IsEnum)
                throw new ArgumentException();
            FieldInfo[] fields = type.GetFields();
            var field = fields
                            .SelectMany(f => f.GetCustomAttributes(
                                typeof(DescriptionAttribute), false), (
                                    f, a) => new { Field = f, Att = a })
                            .Where(a => ((DescriptionAttribute)a.Att)
                                .Description == description).SingleOrDefault();
            return field == null ? default(T) : (T)field.Field.GetRawConstantValue();
        }
    }

    public enum TransactionTypeEnum
    {
        [Display(Name = "In")]
        In = 1,
        [Display(Name = "Out")]
        Out = 2
    }
}
